package com.zhimi.tools.mp3extractor;

import java.io.BufferedReader;
import java.io.FileReader;
import java.util.HashMap;

public class EmmaParser {
	
	HashMap<String, String> wordsMap;
	EmmaParser() {
		wordsMap = new HashMap<String, String>();
	}
	
	// 加载并解析lemma.txt文件，并生成wordsMap索引表
	// 参数filename为 lemma.txt的全路径
	void loadEmmaFile(String filename) throws Exception {
		BufferedReader br = new BufferedReader(new FileReader(filename));
		String line = br.readLine();
		while (line!= null) {
			String[] words = line.trim().split(",");	
			if (words.length > 0) {
				String value = words[0].trim();
				wordsMap.put(value, value); 
				for(String key : words) {
					wordsMap.put(key.trim(), value); 
				}
			}
			line = br.readLine();	
		}
	}
	
	// 参数rawWord为 需要查找原型的单词
	// 返回值为rawWord的原型，如果没找到原型，则返回null
	String lookupEmmaMap(String rawWord) {
		if (wordsMap.containsKey(rawWord))
			return wordsMap.get(rawWord);	
		return null;
	}
	
	public static void main(String args[]) throws Exception {
		EmmaParser emmaParse = new EmmaParser();
		emmaParse.loadEmmaFile("lemma.txt");
		
		System.out.println(emmaParse.lookupEmmaMap("bought"));
		System.out.println(emmaParse.lookupEmmaMap("hitting"));
		System.out.println(emmaParse.lookupEmmaMap("hit"));
	}
	
	
	
	
}
